package de.tu_darmstadt.gdi1.xcom.tests.testcases.tutors;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1;

/**
 * 
 * Tests if mouseclicks on the gamearea are processed correctly.
 * 
 * @author Lukas Schwoebel
 *
 */
public class MouseclickTest {
	
	private XcomTestAdapterLevel1 testAdapter;
	private XcomTestAdapterLevel1 testAdapterMouse;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel1();
		testAdapterMouse = new XcomTestAdapterLevel1();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#clickPerformedAt()}.
	 */
	@Test
	public final void testProcessCorrectly() {

		String representationOfLevel;
		
		// do several clicks to turn the player (9,2) west
		
		testAdapter.loadLevelFromString(TestLevel.testLevel);
		representationOfLevel = testAdapter.levelRepresentationAsString();
		// do mouseclick so the player turns west
		testAdapter.clickPerformedAt(8, 2);
		assertEquals("After click at 8:2 player at 9:2 should look West:\n"+representationOfLevel, 4, testAdapter.activePlayerLookingToDirection());

		testAdapter.loadLevelFromString(TestLevel.testLevel);
		// do mouseclick so the player turns west
		testAdapter.clickPerformedAt(7, 2);
		assertEquals("After click at 7:2 player at 9:2 should look West:\n"+representationOfLevel, 4, testAdapter.activePlayerLookingToDirection());

		testAdapter.loadLevelFromString(TestLevel.testLevel);
		// do mouseclick so the player turns west
		testAdapter.clickPerformedAt(4, 2);
		assertEquals("After click at 4:2 player at 9:2 should look West:\n"+representationOfLevel, 4, testAdapter.activePlayerLookingToDirection());
		

		// now "walk into a wall"
		representationOfLevel = testAdapter.levelRepresentationAsString();
		testAdapter.clickPerformedAt(9, 1);
		assertEquals("After click at 9:1, nothing should have changed, as the player cant walk into a wall:\n"+representationOfLevel, representationOfLevel, testAdapter.levelRepresentationAsString());

		// now click south on player, who is / should be looking north. this should not change anything - or actionpoints should be decreased correctly
		// = two rotations
		assertEquals("Player at 9:2 should be looking north", 1, testAdapter.activePlayerLookingToDirection());
		int actionPointsBeforeClick = testAdapter.actionPointsLeft();
		testAdapter.clickPerformedAt(9, 3);
		
		int directionOfPlayer = testAdapter.activePlayerLookingToDirection();
		boolean clickSouthProcessed = (directionOfPlayer == 1) || (testAdapter.actionPointsLeft() == (actionPointsBeforeClick-2) && directionOfPlayer == 3);
		
		assertEquals("After click at 9:3 (south of player) who was looking north, he still should be\n" +
				"If not, his actionpoints should be decreased by two and he should look south.", true, clickSouthProcessed);	
	}
	

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#clickPerformedAt()}.
	 */
	@Test
	public final void testRotationsDoneCorrectly() {
		
		testAdapter.loadLevelFromString(TestLevel.testLevel);
		
		// clicking at the same field (2|2) for every player, looking at the result
		
		testAdapter.clickPerformedAt(2, 2);
		assertEquals("After click at 2:2 the first player (9:2) should look west", 4, testAdapter.activePlayerLookingToDirection());
		
		// change player by clicking at him
		testAdapter.clickPerformedAt(4, 3);
		testAdapter.clickPerformedAt(2, 2);
		assertEquals("After click at 2:2 the second player (4:3) should look west", 4, testAdapter.activePlayerLookingToDirection());

		// change player by clicking at him
		testAdapter.clickPerformedAt(1, 4);
		testAdapter.clickPerformedAt(2, 2);
		assertEquals("After click at 2:2 the third player (1:4) still should look north", 1, testAdapter.activePlayerLookingToDirection());
		
		// change player by clicking at him
		testAdapter.clickPerformedAt(11, 4);
		testAdapter.clickPerformedAt(2, 2);
		assertEquals("After click at 2:2 the second player (11:4) should look west", 4, testAdapter.activePlayerLookingToDirection());
	}
}
